from Map import Map, Square
from Kings import King, Kings
from Tools import Point

import sys
import pickle
import cProfile
from PIL import Image, ImageDraw, ImageFont
import random

UseMapSave = True
UseKingsSave = True

def load():    
    if UseMapSave :
        if not UseKingsSave :
            output = open('map.pkl', 'rb')
            _map = pickle.load(output)
            output.close()       
    else :
        _map = Map()
        
        _map.GetAllSquares()
        
        output = open('map.pkl', 'wb')
        pickle.dump(_map, output, pickle.HIGHEST_PROTOCOL)
        output.close()
        
    if UseKingsSave :
        output = open('kings.pkl', 'rb')
        _kings = pickle.load(output)
        _map = pickle.load(output)
        output.close()       
    else :
        _kings = Kings()
        
        for x in range(_map.xUL, _map.xUL+_map.width+1) :
            for y in range(_map.yUL, _map.yUL+_map.height+1) :
                s = _map.GetSquare(x, y)
                if s.king == 0 :
                    continue
                
                king = _kings.GetKing(s.king)
                
                if king.neutral :
                    s.king = 0
                    continue
                
                try :
                    king.squaresDict
                except AttributeError :
                    king.squaresDict = {}
                    
                king.squaresDict[Point(x,y)] = s
        
        output = open('kings.pkl', 'wb')
        pickle.dump(_kings, output, pickle.HIGHEST_PROTOCOL)
        pickle.dump(_map, output, pickle.HIGHEST_PROTOCOL)
        output.close()
    
    return (_map, _kings)

def main():
    (_map, _kings) = load()
    
    print "There are %d / %d non neutral kings." % (len([i for (i,j) in _kings.kings.items() if not j.neutral]), len(_kings.kings))
    
    scale = 8
    
    im = Image.new("RGB", (_map.width*scale, _map.height*scale), (255,255,255))

    for (i,k) in _kings.kings.items() :
        if k.neutral :
            continue

        col = (random.randrange(128+64)+64, 0, 0)

        nbS = 0
        xM = 0
        yM = 0

        for (j,s) in k.squaresDict.items() :
            xM += s.x
            yM += s.y
            nbS += 1

            for i in range(scale) :
                for j in range(scale) :
                    im.putpixel((scale*(s.x-_map.xUL)+i, scale*(s.y-_map.yUL)+j), col)
        
        k.nbS = nbS
        k.xM = xM
        k.yM = yM


    colT = (0, 255, 255)
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype("arial.ttf", 15)

    for (i,k) in _kings.kings.items() :
        if k.neutral :
            continue
        
        draw.text((scale*(k.xM/k.nbS-_map.xUL), scale*(k.yM/k.nbS-_map.yUL)), k.nickname, font=font, fill=colT)

    del draw
    
    im.save("map.png")
    
if __name__=="__main__" :
    #cProfile.run('main()')
    main()
    
